import * as api from '../services/index'

export default {
    namespace: 'dva',
    state: {
        list: [],
        total: null,
        limit: 5,
        page: 1,
        loading: true
    },
    reducers: {
        save(state, action) {
            return {
                ...state,
                ...action.payload
            }
        }
    },
    effects: {
        *getList({ payload: {} }, { call, put, select }) {
            const dva = yield select(state => state.dva);
            // const { data } = yield call(api.getList, { limit: dva.limit, page: dva.page });
            // if (data.code === 0) {}
            // 改变pageSize
            if (dva.limit == 5) {
                if (dva.page == 1) {
                    yield put({
                        type: 'save',
                        payload: {
                            list: [{
                                key: '1',
                                name: 'John Brown',
                                IDcard: '610323199212030988',
                                house: 'New York No. 1 Lake Park',
                                identity: 'Renter'
                            }, {
                                key: '2',
                                name: 'Jim Green',
                                IDcard: '610323199212030948',
                                house: 'London No. 1 Lake Park',
                                identity: 'Owner'
                            }, {
                                key: '3',
                                name: 'Green',
                                IDcard: '610323199212030918',
                                house: 'Sidney No. 2 Lake Park',
                                identity: 'Visitor'
                            }, {
                                key: '4',
                                name: 'Black',
                                IDcard: '610323199212030947',
                                house: 'Sidney No. 3 Lake Park',
                                identity: 'Property'
                            }, {
                                key: '5',
                                name: 'Red',
                                IDcard: '610323199212030087',
                                house: 'Sidney No. 4 Lake Park',
                                identity: 'Property'
                            }],
                            total: 8,
                            limit:dva.limit,
                            page:dva.page
                        }
                    })
                } else {
                    yield put({
                        type: 'save',
                        payload: {
                            list: [{
                                key: '6',
                                name: 'Green',
                                IDcard: '610323199212030918',
                                house: 'Sidney No. 2 Lake Park',
                                identity: 'Visitor'
                            }, {
                                key: '7',
                                name: 'Black',
                                IDcard: '610323199212030947',
                                house: 'Sidney No. 3 Lake Park',
                                identity: 'Owner'
                            }, {
                                key: '8',
                                name: 'Red',
                                IDcard: '610323199212030087',
                                house: 'Sidney No. 4 Lake Park',
                                identity: 'Renter'
                            }],
                            total: 8,
                            limit:dva.limit,
                            page:dva.page
                        }
                    })
                }
            } else if (dva.limit > 5) {
                if (dva.page == 1) {
                    yield put({
                        type: 'save',
                        payload: {
                            list: [{
                                key: '1',
                                name: 'John Brown',
                                IDcard: '610323199212030988',
                                house: 'New York No. 1 Lake Park',
                                identity: 'Renter'
                            }, {
                                key: '2',
                                name: 'Jim Green',
                                IDcard: '610323199212030948',
                                house: 'London No. 1 Lake Park',
                                identity: 'Owner'
                            }, {
                                key: '3',
                                name: 'Green',
                                IDcard: '610323199212030918',
                                house: 'Sidney No. 2 Lake Park',
                                identity: 'Visitor'
                            }, {
                                key: '4',
                                name: 'Black',
                                IDcard: '610323199212030947',
                                house: 'Sidney No. 3 Lake Park',
                                identity: 'Property'
                            }, {
                                key: '5',
                                name: 'Red',
                                IDcard: '610323199212030087',
                                house: 'Sidney No. 4 Lake Park',
                                identity: 'Property'
                            }, {
                                key: '6',
                                name: 'Green',
                                IDcard: '610323199212030918',
                                house: 'Sidney No. 2 Lake Park',
                                identity: 'Visitor'
                            }, {
                                key: '7',
                                name: 'Black',
                                IDcard: '610323199212030947',
                                house: 'Sidney No. 3 Lake Park',
                                identity: 'Owner'
                            }, {
                                key: '8',
                                name: 'Red',
                                IDcard: '610323199212030087',
                                house: 'Sidney No. 4 Lake Park',
                                identity: 'Renter'
                            }],
                            total: 8,
                            limit:dva.limit,
                            page:dva.page
                        }
                    })
                }else{
                    yield put({
                        type: 'save',
                        payload: {
                            list:[],
                            total:8,
                            limit:dva.limit,
                            page:dva.page
                        }
                    })
                }
            }
            yield put({
                type:'save',
                payload:{
                    loading:false
                }
            })
        },
        *pageChange({ payload: { limit, page } }, { put}) {
            yield put({
                type: 'save',
                payload: {
                    limit,
                    page,
                    loading: true
                }
            })
            yield put({
                type: 'getList',
                payload: {
                    limit,
                    page
                }
            })
        }
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if (pathname === '/dva') {
                    dispatch({
                        type: 'save',
                        payload: {
                            list: [],
                            total: null,
                            limit: 5,
                            page: 1,
                            loading: true
                        }
                    })
                    dispatch({
                        type: 'getList',
                        payload: {}
                    })
                }
            })
        }
    }
}